<template>
    <!-- 新增/取消分享 -->
    <el-dialog :title="add?'新增分享人':'取消分享人'" :visible.sync="visible" :close-on-click-modal="false" @close="onClose"
        width="30%">
        <el-form :model="form" ref="form" label-width="160px" :rules="rules" size="small">
            <!-- 新增表单项 -->
            <el-row type="flex" justify="left">
                <el-form-item :label="'选择要'+(add?'新增分享的用户':'取消分享的用户')">
                    <el-select v-model="form.userIdList" multiple collapse-tags style="margin-left: 20px;"
                        placeholder="请选择">
                        <el-option v-for="item in validSelectionList" :key="item.userId"
                            :label="item.userName + '(' + item.nickName+')'" :value="item.userId"
                            :disabled="item.disabled">
                        </el-option>
                    </el-select>
                </el-form-item>

            </el-row>
            <el-form-item size="large" style="width:100%;text-align: left;">
                <el-button size="small" type="primary" @click="onSubmit">确定</el-button>
                <el-button size="small" @click="onClose">关闭</el-button>
            </el-form-item>
        </el-form>
    </el-dialog>
</template>

<script>
import request from '@/utils/request'

export default {
    name: "shareDialog",
    props: {
        dialogVisible: {
            type: Boolean,
            default: false
        }
        ,add:{
            type:Boolean,
            default:false
        }
        ,file:{
            type:Object,
            default:()=>{}
        }
    }
    , data() {
        return {
            form: {
                fileId:this.file.id
                , userIdList:[]
                , isAdd:this.add
            }
            , rules: {
            }
            , visible: this.dialogVisible
            , validSelectionList:[]
        }
    }
    , methods: {
        onClose() {            
            this.visible = false;
            this.$emit('close');
        }
        , onSubmit() {
            this.$refs.form.validate(valid => {
                if (valid) {
                    this.isSubmit = true;
                    this.submitRequest().then(res => {
                        if (res.code === 200) {
                            this.$modal.msgSuccess(this.add?'分享成功。':'取消成功。');
                            this.onClose();
                        } else {
                            this.$modal.msgError(res.msg);
                        }
                    }).catch(err => {
                        console.log(err);
                    }).finally(() => {
                        this.toggleButtons();
                    })
                } else {
                    this.$modal.msgError("请输入必填项/有效值。");
                }

            });
        }
        , submitRequest() {
            return request({
                url: '/file-share/submit-share'
                , method: 'post'
                , data: this.form
            })
        }
        ,getValidSelectionList(){
            request({
                url:'/file-share/share-list'
                ,method:'get'
                , params: { fileId: this.form.fileId, isAdd:this.form.isAdd}
            })
            .then(res=>{
                this.validSelectionList = [];
                if(res&&res.code===200){
                    var supplierUsers = this.getSupplierUidData();
                    for (let it of res.data) {
                        if (this.add && supplierUsers.indexOf(it.userId) === -1) {
                            continue;
                        }
                        this.validSelectionList.push({
                            userId: it.userId,
                            userName: it.userName,
                            nickName: it.nickName,
                            disabled: it.disabled,
                        })
                    }
                }
            })
            .catch(error=>{
                console.log(error);
            })

        }
        , toggleButtons() {
        }
        , onClear() {
            this.$refs.form.resetFields();
        }
        , getSupplierUidData() {
            var listStr = localStorage.getItem('supplier-list');
            // 判断listStr是否为空
            if (listStr && listStr.length !== 0) {
                // 将字符串转换为数组
                return JSON.parse(listStr).map(item => item.sysUserId);
            }
            return [];
        }

    }
    , mounted() {
        this.getValidSelectionList();
    }
}
</script>

<style scoped></style>